package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huike.entity.Menu;
import com.huike.entity.Roles;
import com.huike.mapper.RolesMapper;
import com.huike.service.IRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lwen
 * @since 2019-12-28
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements IRolesService {
    public List<Roles> getRolesByUserId(String user_id){
        QueryWrapper<Roles> rolesQueryWrapper = new QueryWrapper<>();
        rolesQueryWrapper.eq("user_id",user_id);
        return baseMapper.selectList(rolesQueryWrapper);
    }

    @Override
    public boolean insertRole(List<Roles> data) {
        return this.saveBatch(data);
    }

    @Override
    public boolean deleteRole(List<Roles> data) {
        for (Roles r:data){
            QueryWrapper<Roles> queryWrapper = new QueryWrapper<Roles>();
            queryWrapper.eq("user_id",r.getUserId());
            queryWrapper.eq("menu_id",r.getMenuId());
            int i = baseMapper.delete(queryWrapper);
            if (i==-1){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<Roles> getRoles(List<String> strs, String user_id) {
        List<Roles> rolesList = new ArrayList<>();
        for (String s:strs){
            Roles roles = new Roles();
            roles.setMenuId(s);
            roles.setUserId(user_id);
            rolesList.add(roles);
        }
        return rolesList;
    }
}
